import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/department_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/execute_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_list/job_ticket/common/common_work_all/common_work_all_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';
import 'package:get/get.dart';

class RoadJobApplicationController extends GetxController {
  //作业总页面controller
  final workAllPageController = Get.find<CommonWorkAllController>();

  //作业申请实体
  ExecuteTicketReceiptList executeTicketReceipt = ExecuteTicketReceiptList();

  //作业负责人
  ExecuteTicketReceiptListOperatorLists principal =
      ExecuteTicketReceiptListOperatorLists();

  //编制人
  ExecuteTicketReceiptListOperatorLists prepared =
      ExecuteTicketReceiptListOperatorLists();

  //监护人
  ExecuteTicketReceiptListOperatorLists guardian =
      ExecuteTicketReceiptListOperatorLists();

  //选中的涉及相关单位名称
  String involvedUnitChecked = '';

  @override
  void onInit() {
    super.onInit();
    //初始化页面列表
    initData();
  }

  ///初始化数据
  initData() async {
    //获取数据作业申请数据
    final res = await HttpUtil().generateGetRequest<ExecuteTicketReceiptList>(
        Interface.getTicketReceiptDetail,
        queryParameters: {
          'ticketReceiptId': workAllPageController.executeEntity.ticketReceiptId
        }).execute();
    res.onSuccess((result) {
      workAllPageController.executeEntity.ticketReceiptList = result.data;
    });
    //赋值
    executeTicketReceipt =
        workAllPageController.executeEntity.ticketReceiptList!;
    //逗号隔开的相关单位名字展示
    involvedUnitChecked = executeTicketReceipt.workOtherDataDTO!.involveList!
        .map((e) => e.involveUnitName)
        .join(',');
    //查找已选中的人员
    for (var item in executeTicketReceipt.operatorLists!) {
      //作业负责人
      if (item.operatorType == 1) {
        principal = item;
      }
      //编制人
      if (item.operatorType == 3) {
        prepared = item;
      }
      //监护人
      if (item.operatorType == 2) {
        guardian = item;
      }
    }
    update();
  }

  ///作业单位选取
  void applyUnitSelect() {
    Get.toNamed(AppRoutes.unitSelection, arguments: {
      'queryParameters': {'type': 2},
      'isRadio': true,
    })?.then((value) {
      //如果不为空则设置单位信息
      if (value != null) {
        value as List<DepartmentEntity>;
        executeTicketReceipt.workDepartmentName = value[0].getShowName();
        executeTicketReceipt.workDepartmentId = value[0].getId();
        executeTicketReceipt.workDepartmentType = value[0].workDepartmentType;
      }
      update();
    });
  }

  ///涉及相关单位选取
  void involvedUnitSelect() {
    Get.toNamed(AppRoutes.unitSelection, arguments: {
      'queryParameters': {'type': 2},
      'isRadio': false,
    })?.then((value) {
      //如果不为空则设置单位信息
      if (value != null) {
        value as List<DepartmentEntity>;
        executeTicketReceipt.workOtherDataDTO!.involveList = [];
        for (var item in value) {
          final Involve tem = Involve()
            ..involveUnitName = item.getShowName()
            ..involveUnitId = item.getId();
          executeTicketReceipt.workOtherDataDTO!.involveList!.add(tem);
        }
        //逗号隔开的相关单位名字展示
        involvedUnitChecked = executeTicketReceipt.workOtherDataDTO!.involveList!
            .map((e) => e.involveUnitName)
            .join(',');
      }
      update();
    });
  }
}
